package main

import (
	"fmt"
	"gitee.com/hxchjm/goge/codegen"
	"gitee.com/hxchjm/goge/codegen/utils/version"
	"github.com/logrusorgru/aurora"
	"github.com/pkg/errors"
	"github.com/urfave/cli"
	"os"
	"runtime"
)

var commands = []cli.Command{
	{
		Name:   "new",
		Usage:  "create new project",
		Action: Create,
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "dir",
				Usage: "the target dir",
			},
		},
	},
	{
		Name:   "proto",
		Usage:  "parse file with protoc and plugin of bm",
		Action: Proto,
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "dir",
				Usage: "the target dir",
			},
			cli.StringSliceFlag{
				Name:  "path",
				Usage: "the path of proto",
			},
		},
	},
}

func Create(ctx *cli.Context) (err error) {
	if err := codegen.CreateCommand(ctx); err != nil {
		return err
	}
	fmt.Println(aurora.Green("Generate Success."))
	return
}

func Proto(ctx *cli.Context) (err error) {
	if err := codegen.ProtoCommand(ctx); err != nil {
		return err
	}
	fmt.Println(aurora.Green("Generate api Success."))
	return
}

func main() {
	app := cli.NewApp()
	app.Usage = "a cli tool to generate code"
	app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH)
	app.Commands = commands

	// cli already print error messages
	if err := app.Run(os.Args); err != nil {
		fmt.Println(aurora.Red(errors.Wrap(err, "app run err")))
		os.Exit(1)
	}
}
